#include "StandardHoughTransformLineDetector.h"

#include <cassert>

using namespace std;

StandardHoughTransformLineDetector::StandardHoughTransformLineDetector(double rhoPrecision, double thetaPrecision, int threshold) {
	this->rhoPrecision    = rhoPrecision;
	this->thetaPrecision  = thetaPrecision;
	this->threshold       = threshold;
}

StandardHoughTransformLineDetector::~StandardHoughTransformLineDetector(void)
{
}

vector<Line> StandardHoughTransformLineDetector::detect(const IplImage* im) {

	vector<Line> vlines;

	assert(im->nChannels == 1); //assume gray

	IplImage* canny = cvCreateImage( cvGetSize(im), 8, 1 );
	CvMemStorage* storage = cvCreateMemStorage(0);
	CvSeq* lines = 0;
	cvCanny( im, canny, 50, 200, 3 );

    lines = cvHoughLines2( canny, storage, CV_HOUGH_STANDARD, rhoPrecision, thetaPrecision, threshold);
   
	for(int i = 0; i < lines->total; i++ ){
        float* line  = (float*)cvGetSeqElem(lines,i);
        float  rho    = line[0];
        float  theta  = line[1];

		vlines.push_back(Line(rho, theta, cvGetSize(im)));
	}

	//cvNamedWindow("canny", 1);
	//cvShowImage("canny", dst);

	//IplImage* im1 = cvCloneImage(im);
	//drawLines(im1, vlines);
	//cvNamedWindow("before", 1);
	//cvShowImage("before", im1);

	return vlines;
}
